﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Enemy : MonoBehaviour
{
    public int life;
    public float speed;
    private Animator animator;
    public int coin;//怪物掉落金币数

    private void Start()
    {
        animator = GetComponent<Animator>();
    }
    void Update()
    {
        transform.position += new Vector3(speed, 0, 0) * Time.deltaTime;
            
    }

    public void GetHit()//受伤
    {
        animator.SetTrigger("hit");
        life -= GameObject.Find("游戏系统").GetComponent<AttackManager>().damage; 
        if(life <= 0 )
        {
            Dead();
        }
    }
    public void Dead()
    {
        Destroy(this.gameObject);
        GameObject.Find("游戏系统").GetComponent<GoldCoin>().coin_num += coin;
    }
    private void OnTriggerEnter2D(Collider2D collision)
    {
        if(collision.gameObject.tag == "attack")
        {
            GetHit();
        }
    }
}
